﻿using System;

namespace PublicDomain.Data.Csv
{
    public static class CsvDataRowGetSafeColumnValue
    {
        public static DateTimeOffset GetSafeDateTimeOffset(this CsvDataRow csvDataRow, int i, DateTimeOffset dbNullDefault = default(DateTimeOffset))
        {
            if (csvDataRow == null)
            {
                throw new ArgumentNullException("csvDataRow");
            }

            if (csvDataRow.IsDBNull(i))
            {
                return dbNullDefault;
            }

            return csvDataRow.GetDateTimeOffset(i);
        }

        public static DateTimeOffset? GetSafeNullableDateTimeOffset(this CsvDataRow csvDataRow, int i, DateTimeOffset? dbNullDefault = null)
        {
            if (csvDataRow == null)
            {
                throw new ArgumentNullException("csvDataRow");
            }

            if (csvDataRow.IsDBNull(i))
            {
                return dbNullDefault;
            }

            return new DateTimeOffset?(csvDataRow.GetDateTimeOffset(i));
        }

        public static TimeSpan GetSafeTimeSpan(this CsvDataRow csvDataRow, int i, TimeSpan dbNullDefault = default(TimeSpan))
        {
            if (csvDataRow == null)
            {
                throw new ArgumentNullException("csvDataRow");
            }

            if (csvDataRow.IsDBNull(i))
            {
                return dbNullDefault;
            }

            return csvDataRow.GetTimeSpan(i);
        }

        public static TimeSpan? GetSafeNullableTimeSpan(this CsvDataRow csvDataRow, int i, TimeSpan? dbNullDefault = null)
        {
            if (csvDataRow == null)
            {
                throw new ArgumentNullException("csvDataRow");
            }

            if (csvDataRow.IsDBNull(i))
            {
                return dbNullDefault;
            }

            return new TimeSpan?(csvDataRow.GetTimeSpan(i));
        }

#pragma warning disable 3001, 3002
        public static sbyte GetSafeSByte(this CsvDataRow csvDataRow, int i, sbyte dbNullDefault = default(sbyte))
        {
            if (csvDataRow == null)
            {
                throw new ArgumentNullException("csvDataRow");
            }

            if (csvDataRow.IsDBNull(i))
            {
                return dbNullDefault;
            }

            return csvDataRow.GetSByte(i);
        }

        public static sbyte? GetSafeNullableSByte(this CsvDataRow csvDataRow, int i, sbyte? dbNullDefault = null)
        {
            if (csvDataRow == null)
            {
                throw new ArgumentNullException("csvDataRow");
            }

            if (csvDataRow.IsDBNull(i))
            {
                return dbNullDefault;
            }

            return new sbyte?(csvDataRow.GetSByte(i));
        }

        public static ushort GetSafeUInt16(this CsvDataRow csvDataRow, int i, ushort dbNullDefault = default(ushort))
        {
            if (csvDataRow == null)
            {
                throw new ArgumentNullException("csvDataRow");
            }

            if (csvDataRow.IsDBNull(i))
            {
                return dbNullDefault;
            }

            return csvDataRow.GetUInt16(i);
        }

        public static ushort? GetSafeNullableUInt16(this CsvDataRow csvDataRow, int i, ushort? dbNullDefault = null)
        {
            if (csvDataRow == null)
            {
                throw new ArgumentNullException("csvDataRow");
            }

            if (csvDataRow.IsDBNull(i))
            {
                return dbNullDefault;
            }

            return new ushort?(csvDataRow.GetUInt16(i));
        }

        public static uint GetSafeUInt32(this CsvDataRow csvDataRow, int i, uint dbNullDefault = default(uint))
        {
            if (csvDataRow == null)
            {
                throw new ArgumentNullException("csvDataRow");
            }

            if (csvDataRow.IsDBNull(i))
            {
                return dbNullDefault;
            }

            return csvDataRow.GetUInt32(i);
        }

        public static uint? GetSafeNullableUInt32(this CsvDataRow csvDataRow, int i, uint? dbNullDefault = null)
        {
            if (csvDataRow == null)
            {
                throw new ArgumentNullException("csvDataRow");
            }

            if (csvDataRow.IsDBNull(i))
            {
                return dbNullDefault;
            }

            return new uint?(csvDataRow.GetUInt32(i));
        }

        public static ulong GetSafeUInt64(this CsvDataRow csvDataRow, int i, ulong dbNullDefault = default(ulong))
        {
            if (csvDataRow == null)
            {
                throw new ArgumentNullException("csvDataRow");
            }

            if (csvDataRow.IsDBNull(i))
            {
                return dbNullDefault;
            }

            return csvDataRow.GetUInt64(i);
        }

        public static ulong? GetSafeNullableUInt64(this CsvDataRow csvDataRow, int i, ulong? dbNullDefault = null)
        {
            if (csvDataRow == null)
            {
                throw new ArgumentNullException("csvDataRow");
            }

            if (csvDataRow.IsDBNull(i))
            {
                return dbNullDefault;
            }

            return new ulong?(csvDataRow.GetUInt64(i));
        }
#pragma warning restore 3001, 3002
    }
}
